
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>[创建进程] · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-katex/katex.min.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-emphasize/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-prism/prism-tomorrow.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    


    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="part5.html" />
    
    
    <link rel="prev" href="part3.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../chapter0/introduction.html">
            
                <a href="../chapter0/introduction.html">
            
                    
                    第零章：实验环境说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../chapter1/introduction.html">
            
                <a href="../chapter1/introduction.html">
            
                    
                    第一章：独立式可执行程序
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter1/part1.html">
            
                <a href="../chapter1/part1.html">
            
                    
                    [安装 nightly rust]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter1/part2.html">
            
                <a href="../chapter1/part2.html">
            
                    
                    使用包管理器 cargo 创建 rust binary 项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../chapter1/part3.html">
            
                <a href="../chapter1/part3.html">
            
                    
                    移除标准库依赖
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../chapter1/part4.html">
            
                <a href="../chapter1/part4.html">
            
                    
                    [移除 runtime 依赖]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../chapter1/part5.html">
            
                <a href="../chapter1/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../chapter2/introduction.html">
            
                <a href="../chapter2/introduction.html">
            
                    
                    第二章：最小化内核
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter2/part1.html">
            
                <a href="../chapter2/part1.html">
            
                    
                    使用目标三元组描述目标平台
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter2/part2.html">
            
                <a href="../chapter2/part2.html">
            
                    
                    编译、生成内核镜像
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter2/part3.html">
            
                <a href="../chapter2/part3.html">
            
                    
                    使用链接脚本指定内存布局
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../chapter2/part4.html">
            
                <a href="../chapter2/part4.html">
            
                    
                    [重写程序入口点 -start]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../chapter2/part5.html">
            
                <a href="../chapter2/part5.html">
            
                    
                    [使用 Qemu 运行内核]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../chapter2/part6.html">
            
                <a href="../chapter2/part6.html">
            
                    
                    封装 SBI 接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../chapter2/part7.html">
            
                <a href="../chapter2/part7.html">
            
                    
                    [实现格式化输出]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../chapter2/part8.html">
            
                <a href="../chapter2/part8.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../chapter3/introduction.html">
            
                <a href="../chapter3/introduction.html">
            
                    
                    第三章：中断
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../chapter3/part1.html">
            
                <a href="../chapter3/part1.html">
            
                    
                    rv64 中断介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../chapter3/part2.html">
            
                <a href="../chapter3/part2.html">
            
                    
                    [手动触发断点中断]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../chapter3/part3.html">
            
                <a href="../chapter3/part3.html">
            
                    
                    程序运行上下文环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../chapter3/part4.html">
            
                <a href="../chapter3/part4.html">
            
                    
                    [实现上下文环境保存与恢复]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../chapter3/part5.html">
            
                <a href="../chapter3/part5.html">
            
                    
                    [时钟中断]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../chapter3/part6.html">
            
                <a href="../chapter3/part6.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../chapter4/introduction.html">
            
                <a href="../chapter4/introduction.html">
            
                    
                    第四章：内存管理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../chapter4/part1.html">
            
                <a href="../chapter4/part1.html">
            
                    
                    [物理内存探测与管理]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../chapter4/part2.html">
            
                <a href="../chapter4/part2.html">
            
                    
                    [动态内存分配]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../chapter4/part3.html">
            
                <a href="../chapter4/part3.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../chapter5/introduction.html">
            
                <a href="../chapter5/introduction.html">
            
                    
                    第五章：内存虚拟化
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter5/part1.html">
            
                <a href="../chapter5/part1.html">
            
                    
                    页表：从虚拟内存到物理内存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter5/part2.html">
            
                <a href="../chapter5/part2.html">
            
                    
                    [内核初始映射]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../chapter5/part3.html">
            
                <a href="../chapter5/part3.html">
            
                    
                    内核重映射
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../chapter5/part4.html">
            
                <a href="../chapter5/part4.html">
            
                    
                    内核重映射实现之一：页表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../chapter5/part5.html">
            
                <a href="../chapter5/part5.html">
            
                    
                    内核重映射实现之二：MemorySet
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6" data-path="../chapter5/part6.html">
            
                <a href="../chapter5/part6.html">
            
                    
                    [内核重映射实现之三：完结]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.7" data-path="../chapter5/part7.html">
            
                <a href="../chapter5/part7.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter6/introduction.html">
            
                <a href="../chapter6/introduction.html">
            
                    
                    第六章：内核线程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../chapter6/part1.html">
            
                <a href="../chapter6/part1.html">
            
                    
                    线程状态与保存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../chapter6/part2.html">
            
                <a href="../chapter6/part2.html">
            
                    
                    线程切换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../chapter6/part3.html">
            
                <a href="../chapter6/part3.html">
            
                    
                    内核线程初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../chapter6/part4.html">
            
                <a href="../chapter6/part4.html">
            
                    
                    [内核线程创建与切换测试]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../chapter6/part5.html">
            
                <a href="../chapter6/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../chapter7/introduction.html">
            
                <a href="../chapter7/introduction.html">
            
                    
                    第七章：线程调度
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../chapter7/part1.html">
            
                <a href="../chapter7/part1.html">
            
                    
                    线程池与线程管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../chapter7/part2.html">
            
                <a href="../chapter7/part2.html">
            
                    
                    内核调度线程 idle
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../chapter7/part3.html">
            
                <a href="../chapter7/part3.html">
            
                    
                    线程调度之 Round Robin 算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="../chapter7/part4.html">
            
                <a href="../chapter7/part4.html">
            
                    
                    [线程调度测试]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.5" data-path="../chapter7/part5.html">
            
                <a href="../chapter7/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="introduction.html">
            
                <a href="introduction.html">
            
                    
                    第八章：进程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="part1.html">
            
                <a href="part1.html">
            
                    
                    [编写用户程序]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="part1_1.html">
            
                <a href="part1_1.html">
            
                    
                    合并内核与应用程序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="part2.html">
            
                <a href="part2.html">
            
                    
                    在内核中实现系统调用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="part3.html">
            
                <a href="part3.html">
            
                    
                    创建虚拟内存空间
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.10.5" data-path="part4.html">
            
                <a href="part4.html">
            
                    
                    [创建进程]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.6" data-path="part5.html">
            
                <a href="part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../chapter9/introduction.html">
            
                <a href="../chapter9/introduction.html">
            
                    
                    第九章：文件系统
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="../chapter9/part1.html">
            
                <a href="../chapter9/part1.html">
            
                    
                    [使用文件系统]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="../chapter9/part2.html">
            
                <a href="../chapter9/part2.html">
            
                    
                    [实现记事本]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.3" data-path="../chapter9/part3.html">
            
                <a href="../chapter9/part3.html">
            
                    
                    [实现终端]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.4" data-path="../chapter9/part4.html">
            
                <a href="../chapter9/part4.html">
            
                    
                    [文件读写]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.5" data-path="../chapter9/part5.html">
            
                <a href="../chapter9/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../chapter10/introduction.html">
            
                <a href="../chapter10/introduction.html">
            
                    
                    第十章：同步互斥
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../exercise/introduction.html">
            
                <a href="../exercise/introduction.html">
            
                    
                    练习题
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.13.1" data-path="../exercise/part1.html">
            
                <a href="../exercise/part1.html">
            
                    
                    1. 中断异常
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.2" data-path="../exercise/part2.html">
            
                <a href="../exercise/part2.html">
            
                    
                    2. 物理内存管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.3" data-path="../exercise/part3.html">
            
                <a href="../exercise/part3.html">
            
                    
                    3. 虚拟内存管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.4" data-path="../exercise/part4.html">
            
                <a href="../exercise/part4.html">
            
                    
                    4. 线程管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.5" data-path="../exercise/part5.html">
            
                <a href="../exercise/part5.html">
            
                    
                    5. 用户进程（+ 虚拟内存管理 + 线程管理）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.6" data-path="../exercise/part6.html">
            
                <a href="../exercise/part6.html">
            
                    
                    6. CPU 调度
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.7" data-path="../exercise/part7.html">
            
                <a href="../exercise/part7.html">
            
                    
                    7. 同步互斥
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.8" data-path="../exercise/part8.html">
            
                <a href="../exercise/part8.html">
            
                    
                    8. 文件系统
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../appendix/introduction.html">
            
                <a href="../appendix/introduction.html">
            
                    
                    附录
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.14.1" data-path="../appendix/inline_asm.html">
            
                <a href="../appendix/inline_asm.html">
            
                    
                    内联汇编
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.2" data-path="../appendix/install_rust.html">
            
                <a href="../appendix/install_rust.html">
            
                    
                    安装 rust
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >[创建进程]</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x521B;&#x5EFA;&#x5E76;&#x8FD0;&#x884C;&#x8FDB;&#x7A0B;">&#x521B;&#x5EFA;&#x5E76;&#x8FD0;&#x884C;&#x8FDB;&#x7A0B;</h2>
<ul>
<li><a href="https://github.com/rcore-os/rCore_tutorial/tree/ch8-pa4" target="_blank">&#x4EE3;&#x7801;</a></li>
</ul>
<p>&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x80FD;&#x5EFA;&#x7ACB;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x4E86;&#xFF0C;&#x5185;&#x6838;&#x4E5F;&#x80FD;&#x591F;&#x4E3A;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x5EFA;&#x7ACB;&#x7528;&#x6237;&#x6001;&#x865A;&#x62DF;&#x5185;&#x5B58;&#x7A7A;&#x95F4;&#x4E86;&#x3002;&#x90A3;&#x79BB;<em>&#x5728;&#x81EA;&#x5DF1;&#x7684;&#x5185;&#x6838;&#x4E0A;&#x8DD1;&#x8FD0;&#x884C;&#x5728;&#x7528;&#x6237;&#x6001;&#x7684;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;</em>&#x8FD8;&#x7F3A;&#x5565;&#xFF1F;&#x5176;&#x5B9E;&#x6211;&#x4EEC;&#x5230;&#x4E86;&#x6700;&#x540E;&#x4E00;&#x6B65;--<strong>&#x521B;&#x5EFA;&#x8FDB;&#x7A0B;</strong>&#xFF01;</p>
<blockquote>
<p><strong>[info]&#x7EBF;&#x7A0B;&#x4E0E;&#x8FDB;&#x7A0B;&#x8FDB;&#x9636;</strong></p>
<p>&#x6211;&#x4EEC;&#x5728;<a href="../chapter6/introduction.html">&#x7B2C;&#x516D;&#x7AE0;<strong>&#x5185;&#x6838;&#x7EBF;</strong>&#x5F00;&#x59CB;&#x90E8;&#x5206;</a>&#x7B80;&#x5355;&#x4ECB;&#x7ECD;&#x8FC7;&#x8FDB;&#x7A0B;&#xFF0C;&#x7EBF;&#x7A0B;&#xFF0C;&#x4EE5;&#x53CA;&#x4E8C;&#x8005;&#x7684;&#x5173;&#x7CFB;&#x3002;&#x73B0;&#x5728;&#x8981;&#x5728; OS &#x4E2D;&#x521B;&#x5EFA;&#x8FDB;&#x7A0B;&#x4E86;&#xFF0C;&#x5F53;&#x7136;&#x9700;&#x8981;&#x5BF9;&#x8FDB;&#x7A0B;&#x6709;&#x8FDB;&#x4E00;&#x6B65;&#x7684;&#x6DF1;&#x5165;&#x4E86;&#x89E3;&#x3002;</p>
<p><strong>&#x8FDB;&#x7A0B;</strong>&#x8868;&#x793A;&#x6B63;&#x5728;&#x8FD0;&#x884C;&#x7A0B;&#x5E8F;&#xFF0C;&#x5305;&#x62EC;&#x4EE3;&#x7801;&#xFF0C;&#x6570;&#x636E;&#xFF0C;&#x5806;&#x548C;&#x6808;&#x3002;&#x5728;&#x5927;&#x591A;&#x6570;&#x7684;&#x8FDB;&#x7A0B;&#x5B9E;&#x73B0;&#x4E2D;&#xFF08;&#x4F46;&#x5E76;&#x975E;&#x603B;&#x662F;&#x5982;&#x6B64;&#xFF09;&#xFF0C;&#x6BCF;&#x4E2A;&#x8FDB;&#x7A0B;&#x90FD;&#x6709;&#x81EA;&#x5DF1;&#x7684;&#x865A;&#x62DF;&#x5730;&#x5740;&#x7A7A;&#x95F4;&#xFF08;&#x5373;&#xFF0C;&#x81EA;&#x5DF1;&#x7684;&#x903B;&#x8F91;&#x5730;&#x5740;&#x5230;&#x7269;&#x7406;&#x5185;&#x5B58;&#x7684;&#x6620;&#x5C04;&#xFF09;&#x548C;&#x81EA;&#x5DF1;&#x7684;&#x7CFB;&#x7EDF;&#x8D44;&#x6E90;&#x96C6;&#xFF08;&#x5982;&#x6587;&#x4EF6;&#xFF0C;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x7B49;&#xFF09;&#x3002;&#x6BCF;&#x4E2A;&#x8FDB;&#x7A0B;&#x90FD;&#x6709;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x662F;&#x5F88;&#x666E;&#x904D;&#x7684;&#x3002;&#x8FD9;&#x6837;&#xFF0C;&#x5C31;&#x6709;&#x4E00;&#x4E2A;&#x8FDB;&#x7A0B;&#x6765;&#x7EF4;&#x62A4;&#x5730;&#x5740;&#x7A7A;&#x95F4;&#xFF0C;&#x5E76;&#x6709;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x6765;&#x63A7;&#x5236;&#x8FDB;&#x7A0B;&#x7684;&#x6267;&#x884C;&#x3002;</p>
<p><strong>&#x7EBF;&#x7A0B;</strong>&#x662F;&#x8FDB;&#x7A0B;&#x7684;&#x63A7;&#x5236;&#x6D41;&#x7A0B;&#x3002;&#x7EBF;&#x7A0B;&#x53EF;&#x4EE5;&#x662F;&#x201C;&#x7528;&#x6237;&#x7EA7;&#x522B;&#x201D;&#xFF08;&#x5373;&#x8FDB;&#x7A0B;&#x5904;&#x7406;&#x81EA;&#x8EAB;&#x5185;&#x7684;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#xFF0C;&#x4E0D;&#x7528; OS &#x77E5;&#x9053;&#x4E0E;&#x53C2;&#x4E0E;&#xFF09;&#x6216;&#x201C;&#x5185;&#x6838;&#x7EA7;&#x522B;&#x201D;&#xFF08;&#x5373;&#x901A;&#x8FC7; OS &#x7684;&#x8C03;&#x5EA6;&#x7A0B;&#x5E8F;&#x6765;&#x8C03;&#x5EA6;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x3002;&#x5FD8;&#x4E86;&#xFF1F;&#x56DE;&#x5FC6;&#x4E00;&#x4E0B;<a href="../chapter7/introduction.html">&#x7B2C;&#x4E03;&#x7AE0;<strong>&#x7EBF;&#x7A0B;&#x8C03;&#x5EA6;</strong></a>&#xFF09;&#x3002;&#x6765;&#x81EA;&#x540C;&#x4E00;&#x8FDB;&#x7A0B;&#x7684;&#x4E24;&#x4E2A;&#x7EBF;&#x7A0B;&#x81EA;&#x7136;&#x4F1A;&#x5171;&#x4EAB;&#x76F8;&#x540C;&#x7684;&#x4EE3;&#x7801;&#x548C;&#x5168;&#x5C40;&#x6570;&#x636E;&#x4EE5;&#x53CA;&#x8FDB;&#x7A0B;&#x7684;&#x7CFB;&#x7EDF;&#x8D44;&#x6E90;&#xFF0C;&#x4F46;&#x662F;&#x4F1A;&#x5177;&#x6709;&#x4E0D;&#x540C;&#x7684;&#x5806;&#x6808;&#x3002;&#x4EE5;&#x4F7F;&#x5B83;&#x4EEC;&#x4E0D;&#x4F1A;&#x5E72;&#x6270;&#x5F7C;&#x6B64;&#x7684;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#xFF0C;&#x5E76;&#x4E14;&#x53EF;&#x80FD;&#x5177;&#x6709;&#x81EA;&#x5DF1;&#x7684;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x94FE;&#x3002;</p>
<p>&#x4EE3;&#x8868;&#x8FDB;&#x7A0B;&#x63A7;&#x5236;&#x6D41;&#x7A0B;&#x7684;&#x7EBF;&#x7A0B;&#x4E00;&#x822C;&#x5728;&#x7528;&#x6237;&#x6A21;&#x5F0F;&#xFF08;RISC-V &#x7684; U Mode&#xFF09;&#x4E0B;&#x8FD0;&#x884C;&#x3002;&#x5F53;&#x9700;&#x8981;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x670D;&#x52A1;&#x65F6;&#xFF0C;&#x7EBF;&#x7A0B;&#x4F1A;&#x6267;&#x884C;&#x7CFB;&#x7EDF;&#x670D;&#x52A1;&#x8BF7;&#x6C42;&#x547D;&#x4EE4;&#xFF0C;&#x4ECE;&#x800C;&#x4ECE;&#x7528;&#x6237;&#x6A21;&#x5F0F;&#x5207;&#x6362;&#x5230;&#x4E86;&#x5185;&#x6838;&#x6A21;&#x5F0F;&#xFF08;RISC-V &#x7684; S Mode&#xFF09;&#xFF0C;&#x7531; OS &#x8FDB;&#x884C;&#x5B8C;&#x6210;&#x670D;&#x52A1;&#x540E;&#xFF0C;&#x518D;&#x8FD4;&#x56DE;&#x5230;&#x7528;&#x6237;&#x6A21;&#x5F0F;&#x8BA9;&#x7EBF;&#x7A0B;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#x3002;&#x7531;&#x4E8E; OS &#x8981;&#x5E94;&#x5BF9;&#x4E0D;&#x540C;&#x7EBF;&#x7A0B;&#x7684;&#x8BF7;&#x6C42;&#xFF0C;&#x6240;&#x4EE5;&#x5728;&#x5185;&#x6838;&#x4E2D;&#xFF0C;&#x9700;&#x8981;&#x4E3A;&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x51C6;&#x5907;&#x597D;&#x4E00;&#x4E2A;&#x5185;&#x6838;&#x6A21;&#x5F0F;&#x4E0B;&#x7684;&#x6808;&#x3002;&#x6240;&#x4EE5;&#x5728;&#x7528;&#x6237;&#x6A21;&#x5F0F;&#x4E0B;&#x7684;&#x7EBF;&#x7A0B;&#xFF08;&#x7B80;&#x79F0;<strong>&#x7528;&#x6237;&#x7EBF;&#x7A0B;</strong>&#xFF09;&#x9700;&#x8981;&#x6709;&#x4E24;&#x4E2A;&#x6808;&#xFF08;<strong>&#x7528;&#x6237;&#x6A21;&#x5F0F;&#x6808;&#x548C;&#x5185;&#x6838;&#x6A21;&#x5F0F;&#x6808;</strong>&#xFF09;&#x3002;</p>
</blockquote>
<h3 id="&#x7528;&#x6237;&#x7EBF;&#x7A0B;">&#x7528;&#x6237;&#x7EBF;&#x7A0B;</h3>
<h4 id="&#x521B;&#x5EFA;&#x7528;&#x6237;&#x7EBF;&#x7A0B;&#x4E3B;&#x4F53;">&#x521B;&#x5EFA;&#x7528;&#x6237;&#x7EBF;&#x7A0B;&#x4E3B;&#x4F53;</h4>
<p>&#x7528;&#x6237;&#x7EBF;&#x7A0B;&#x7684;&#x6307;&#x4EE4;&#x6D41;&#x6765;&#x81EA;&#x4E8E;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x7684;&#x4EE3;&#x7801;&#x6BB5;&#xFF0C;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x7B49;&#x6570;&#x636E;&#x6765;&#x81EA;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x7684;&#x6570;&#x636E;&#x6BB5;&#xFF0C;&#x6240;&#x4EE5;&#x9700;&#x8981;&#x89E3;&#x6790;&#x5E94;&#x7528;&#x7A0B;&#x5E8F; ELF &#x6267;&#x884C;&#x6587;&#x4EF6;&#x7684;&#x5185;&#x5BB9;&#xFF0C;&#x83B7;&#x53D6;&#x8FD9;&#x4E9B;&#x5185;&#x5BB9;&#xFF0C;&#x5E76;&#x653E;&#x5230;&#x9875;&#x8868;&#x9879; USER &#x4F4D;&#x5C5E;&#x6027;&#x90FD;&#x662F;<strong>1</strong>&#x7684;&#x865A;&#x62DF;&#x5185;&#x5B58;&#x7A7A;&#x95F4;&#x4E2D;&#xFF08;&#x4E0A;&#x4E00;&#x8282;&#x5C31;&#x662F;&#x5E72;&#x7684;&#x8FD9;&#x4E2A;&#x4E8B;&#x60C5;&#xFF0C;&#x73B0;&#x5728;&#x53EA;&#x9700;&#x8C03;&#x7528;&#x4E00;&#x4E0B;&#x5373;&#x53EF;&#xFF09;&#x3002;&#x7136;&#x540E;&#x518D;&#x521B;&#x5EFA;&#x7528;&#x6237;&#x6A21;&#x5F0F;&#x6808;&#x548C;&#x5185;&#x6838;&#x6A21;&#x5F0F;&#x6808;&#xFF08;&#x6CE8;&#x610F;&#xFF0C;&#x867D;&#x7136;&#x90FD;&#x662F;&#x5185;&#x5B58;&#xFF0C;&#x4F46;&#x5B83;&#x4EEC;&#x7684;&#x9875;&#x8868;&#x9879;&#x7684; USER &#x4F4D;&#x5C5E;&#x6027;&#x662F;&#x4E0D;&#x540C;&#x7684;&#xFF09;&#x3002;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// src/process/structs.rs</span>

<span class="token keyword">impl</span> <span class="token class-name">Thread</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x65B0;&#x5EFA;&#x5185;&#x6838;&#x7EBF;&#x7A0B;</span>
    <span class="token comment">// &#x4F20;&#x5165;&#x53C2;&#x6570;&#x4E3A;&#x94FE;&#x63A5;&#x5728;&#x5185;&#x6838;&#x4E2D;&#x7684;&#x7528;&#x6237;&#x7A0B;&#x5E8F;</span>
    <span class="token keyword">pub</span> <span class="token keyword">unsafe</span> <span class="token keyword">fn</span> <span class="token function-definition function">new_user</span><span class="token punctuation">(</span>data<span class="token punctuation">:</span> <span class="token operator">&amp;</span><span class="token punctuation">[</span><span class="token keyword">u8</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token class-name">Box</span><span class="token operator">&lt;</span><span class="token class-name">Thread</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
        <span class="token comment">// &#x786E;&#x8BA4;&#x5408;&#x6CD5;&#x6027;</span>
        <span class="token keyword">let</span> elf <span class="token operator">=</span> <span class="token class-name">ElfFile</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">expect</span><span class="token punctuation">(</span><span class="token string">&quot;failed to analyse elf!&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">match</span> elf<span class="token punctuation">.</span>header<span class="token punctuation">.</span>pt2<span class="token punctuation">.</span><span class="token function">type_</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">as_type</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token namespace">header<span class="token punctuation">::</span></span><span class="token class-name">Type</span><span class="token punctuation">::</span><span class="token class-name">Executable</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
                <span class="token macro property">println!</span><span class="token punctuation">(</span><span class="token string">&quot;it really a executable!&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token namespace">header<span class="token punctuation">::</span></span><span class="token class-name">Type</span><span class="token punctuation">::</span><span class="token class-name">SharedObject</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
                <span class="token macro property">panic!</span><span class="token punctuation">(</span><span class="token string">&quot;shared object is not supported!&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            _ <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
                <span class="token macro property">panic!</span><span class="token punctuation">(</span><span class="token string">&quot;unsupported elf type!&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token comment">// &#x83B7;&#x53D6;&#x5165;&#x53E3;&#x70B9;</span>
        <span class="token keyword">let</span> entry_addr <span class="token operator">=</span> elf<span class="token punctuation">.</span>header<span class="token punctuation">.</span>pt2<span class="token punctuation">.</span><span class="token function">entry_point</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token keyword">usize</span><span class="token punctuation">;</span>
        <span class="token comment">// &#x4E3A;&#x7528;&#x6237;&#x7A0B;&#x5E8F;&#x521B;&#x5EFA;&#x65B0;&#x7684;&#x865A;&#x62DF;&#x5185;&#x5B58;&#x7A7A;&#x95F4;</span>
        <span class="token keyword">let</span> <span class="token keyword">mut</span> vm <span class="token operator">=</span> elf<span class="token punctuation">.</span><span class="token function">make_memory_set</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// &#x521B;&#x5EFA;&#x7528;&#x6237;&#x6808;</span>
        <span class="token keyword">let</span> <span class="token keyword">mut</span> ustack_top <span class="token operator">=</span> <span class="token punctuation">{</span>
            <span class="token comment">// &#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x5C06;&#x7528;&#x6237;&#x6808;&#x56FA;&#x5B9A;&#x5728;&#x865A;&#x62DF;&#x5185;&#x5B58;&#x7A7A;&#x95F4;&#x4E2D;&#x7684;&#x67D0;&#x4F4D;&#x7F6E;</span>
            <span class="token keyword">let</span> <span class="token punctuation">(</span>ustack_bottom<span class="token punctuation">,</span> ustack_top<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token constant">USER_STACK_OFFSET</span><span class="token punctuation">,</span> <span class="token constant">USER_STACK_OFFSET</span> <span class="token operator">+</span> <span class="token constant">USER_STACK_SIZE</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">// &#x5C06;&#x7528;&#x6237;&#x6808;&#x63D2;&#x5165;&#x865A;&#x62DF;&#x5185;&#x5B58;&#x7A7A;&#x95F4;</span>
            vm<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>
                ustack_bottom<span class="token punctuation">,</span>
                ustack_top<span class="token punctuation">,</span>
                <span class="token comment">// &#x6CE8;&#x610F;&#x8FD9;&#x91CC;&#x8BBE;&#x7F6E;&#x4E3A;&#x7528;&#x6237;&#x6001;</span>
                <span class="token class-name">MemoryAttr</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">set_user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                <span class="token class-name">ByFrame</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                <span class="token class-name">None</span><span class="token punctuation">,</span>
            <span class="token punctuation">)</span><span class="token punctuation">;</span>
            ustack_top
        <span class="token punctuation">}</span><span class="token punctuation">;</span>

        <span class="token comment">// &#x521B;&#x5EFA;&#x5185;&#x6838;&#x6808;</span>
        <span class="token keyword">let</span> kstack <span class="token operator">=</span> <span class="token class-name">KernelStack</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">Box</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span>
            <span class="token class-name">Thread</span> <span class="token punctuation">{</span>
                context<span class="token punctuation">:</span> <span class="token class-name">Context</span><span class="token punctuation">::</span><span class="token function">new_user_thread</span><span class="token punctuation">(</span>entry_addr<span class="token punctuation">,</span> ustack_top<span class="token punctuation">,</span> kstack<span class="token punctuation">.</span><span class="token function">top</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> vm<span class="token punctuation">.</span><span class="token function">token</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                kstack<span class="token punctuation">:</span> kstack<span class="token punctuation">,</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// src/consts.rs</span>

<span class="token keyword">pub</span> <span class="token keyword">const</span> <span class="token constant">USER_STACK_SIZE</span><span class="token punctuation">:</span> <span class="token keyword">usize</span> <span class="token operator">=</span> <span class="token number">0x80000</span><span class="token punctuation">;</span>
<span class="token keyword">pub</span> <span class="token keyword">const</span> <span class="token constant">USER_STACK_OFFSET</span><span class="token punctuation">:</span> <span class="token keyword">usize</span> <span class="token operator">=</span> <span class="token number">0xffffffff00000000</span><span class="token punctuation">;</span>
</code></pre>
<p>&#x73B0;&#x5728;&#x5927;&#x6982;&#x53EF;&#x4EE5;&#x7406;&#x89E3;&#x7528;&#x6237;&#x7EBF;&#x7A0B;&#x4E3A;&#x4F55;&#x5728;&#x4E2D;&#x65AD;&#x65F6;&#x8981;&#x4ECE;&#x7528;&#x6237;&#x6808;&#x5207;&#x6362;&#x5230;&#x5185;&#x6838;&#x6808;&#x4E86;&#x3002;&#x5982;&#x679C;&#x4E0D;&#x5207;&#x6362;&#xFF0C;&#x5185;&#x6838;&#x7684;&#x5904;&#x7406;&#x8FC7;&#x7A0B;&#x4F1A;&#x7559;&#x5728;&#x7528;&#x6237;&#x6808;&#x4E0A;&#xFF0C;&#x4F7F;&#x7528;&#x7528;&#x6237;&#x7A0B;&#x5E8F;&#x53EF;&#x80FD;&#x8BBF;&#x95EE;&#x5230;&#xFF0C;&#x8FD9;&#x663E;&#x7136;&#x662F;&#x5F88;&#x5371;&#x9669;&#x7684;&#x3002;</p>
<h4 id="&#x521D;&#x59CB;&#x5316;&#x5185;&#x6838;&#x6808;">&#x521D;&#x59CB;&#x5316;&#x5185;&#x6838;&#x6808;</h4>
<p>&#x7528;&#x8DDF;&#x5185;&#x6838;&#x7EBF;&#x7A0B;&#x4E00;&#x6837;&#x7684;&#x65B9;&#x6CD5;&#x8FDB;&#x884C;&#x7EBF;&#x7A0B;&#x6808;&#x4E0A;&#x5185;&#x5BB9;&#x7684;&#x521D;&#x59CB;&#x5316;&#xFF0C;&#x6CE8;&#x610F;&#x5207;&#x6362;&#x8FC7;&#x7A0B;&#x603B;&#x662F;&#x5728;&#x5185;&#x6838;&#x6001;&#x6267;&#x884C;&#x7684;&#xFF08;&#x65E0;&#x8BBA;&#x662F;&#x5207;&#x6362;&#x5230; idle &#xFF0C;&#x8FD8;&#x662F;&#x5207;&#x6362;&#x56DE;&#x6765;&#xFF09;&#xFF0C;&#x56E0;&#x6B64;&#x6808;&#x4E0A;&#x7684;&#x5185;&#x5BB9;&#x8981;&#x538B;&#x5230;&#x5185;&#x6838;&#x6808;&#x4E0A;&#x3002;&#x4F46;&#x662F;&#x901A;&#x8FC7; <code>__trapret</code> &#x8FD4;&#x56DE;&#x65F6;&#x5374;&#x8981;&#x5C06; <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mtext><mi mathvariant="normal">s</mi><mi mathvariant="normal">p</mi></mtext></mrow><annotation encoding="application/x-tex">\text{sp}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.43056em;"></span><span class="strut bottom" style="height:0.625em;vertical-align:-0.19444em;"></span><span class="base textstyle uncramped"><span class="mord text textstyle uncramped"><span class="mord mathrm">sp</span></span></span></span></span> &#x8BBE;&#x7F6E;&#x4E3A;&#x7528;&#x6237;&#x6808;&#x3002;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// src/context.rs</span>

<span class="token keyword">impl</span> <span class="token class-name">Context</span> <span class="token punctuation">{</span>
    <span class="token punctuation">...</span>
    <span class="token keyword">pub</span> <span class="token keyword">unsafe</span> <span class="token keyword">fn</span> <span class="token function-definition function">new_user_thread</span><span class="token punctuation">(</span>
        entry<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">,</span>
        ustack_top<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">,</span>
        kstack_top<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">,</span>
        satp<span class="token punctuation">:</span> <span class="token keyword">usize</span>
    <span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">Self</span> <span class="token punctuation">{</span>
        <span class="token comment">// &#x538B;&#x5230;&#x5185;&#x6838;&#x6808;</span>
        <span class="token class-name">ContextContent</span><span class="token punctuation">::</span><span class="token function">new_user_thread</span><span class="token punctuation">(</span>entry<span class="token punctuation">,</span> ustack_top<span class="token punctuation">,</span> satp<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">push_at</span><span class="token punctuation">(</span>kstack_top<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">impl</span> <span class="token class-name">ContextContent</span> <span class="token punctuation">{</span>
    <span class="token keyword">fn</span> <span class="token function-definition function">new_user_thread</span><span class="token punctuation">(</span>
        entry<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">,</span>
        ustack_top<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">,</span>
        satp<span class="token punctuation">:</span> <span class="token keyword">usize</span>
    <span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">Self</span> <span class="token punctuation">{</span>
        <span class="token class-name">ContextContent</span> <span class="token punctuation">{</span>
            ra<span class="token punctuation">:</span> __trapret <span class="token keyword">as</span> <span class="token keyword">usize</span><span class="token punctuation">,</span>
            satp<span class="token punctuation">,</span>
            s<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">;</span> <span class="token number">12</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
            tf<span class="token punctuation">:</span> <span class="token punctuation">{</span>
                <span class="token keyword">let</span> <span class="token keyword">mut</span> tf<span class="token punctuation">:</span> <span class="token class-name">TrapFrame</span> <span class="token operator">=</span> <span class="token keyword">unsafe</span> <span class="token punctuation">{</span> <span class="token function">zeroed</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
                <span class="token comment">// &#x5229;&#x7528; __trapret &#x8FD4;&#x56DE;&#x540E;&#x8BBE;&#x7F6E;&#x4E3A;&#x7528;&#x6237;&#x6808;</span>
                tf<span class="token punctuation">.</span>x<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">=</span> ustack_top<span class="token punctuation">;</span>
                <span class="token comment">// &#x8BBE;&#x7F6E; sepc &#x4ECE;&#x800C;&#x5728; sret &#x4E4B;&#x540E;&#x8DF3;&#x8F6C;&#x5230;&#x7528;&#x6237;&#x7A0B;&#x5E8F;&#x5165;&#x53E3;&#x70B9;</span>
                tf<span class="token punctuation">.</span>sepc <span class="token operator">=</span> entry<span class="token punctuation">;</span>
                tf<span class="token punctuation">.</span>sstatus <span class="token operator">=</span> <span class="token namespace">sstatus<span class="token punctuation">::</span></span><span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                tf<span class="token punctuation">.</span>sstatus<span class="token punctuation">.</span><span class="token function">set_spie</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                tf<span class="token punctuation">.</span>sstatus<span class="token punctuation">.</span><span class="token function">set_sie</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token comment">// &#x8BBE;&#x7F6E; sstatus &#x7684; spp &#x5B57;&#x6BB5;&#x4E3A; User</span>
                <span class="token comment">// &#x4ECE;&#x800C;&#x5728; sret &#x4E4B;&#x540E; CPU &#x7684;&#x7279;&#x6743;&#x7EA7;&#x5C06;&#x53D8;&#x4E3A; U Mode</span>
                tf<span class="token punctuation">.</span>sstatus<span class="token punctuation">.</span><span class="token function">set_spp</span><span class="token punctuation">(</span><span class="token namespace">sstatus<span class="token punctuation">::</span></span><span class="token constant">SPP</span><span class="token punctuation">::</span><span class="token class-name">User</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                tf
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x7684;&#x7528;&#x6237;&#x7EBF;&#x7A0B;&#x5C31;&#x521B;&#x5EFA;&#x5B8C;&#x6BD5;&#x4E86;&#x3002;&#x6211;&#x4EEC;&#x8D76;&#x5FEB;&#x628A;&#x5B83;&#x8DDF;&#x6211;&#x4EEC;&#x4E4B;&#x524D;&#x521B;&#x5EFA;&#x7684;&#x90A3;&#x4E9B;&#x5185;&#x6838;&#x7EBF;&#x7A0B;&#x4E00;&#x8D77;&#x8FD0;&#x884C;&#x4E00;&#x4E0B;&#x5427;&#x3002;</p>
<h4 id="&#x3000;&#x521B;&#x5EFA;&#x7528;&#x6237;&#x7EBF;&#x7A0B;">&#x3000;&#x521B;&#x5EFA;&#x7528;&#x6237;&#x7EBF;&#x7A0B;</h4>
<p>&#x5728;&#x521B;&#x5EFA;&#x5B8C; <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mn>5</mn></mrow><annotation encoding="application/x-tex">5</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.64444em;"></span><span class="strut bottom" style="height:0.64444em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord mathrm">5</span></span></span></span> &#x4E2A;&#x5185;&#x6838;&#x7EBF;&#x7A0B;&#x4E4B;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x81EA;&#x5DF1;&#x7684;&#x7528;&#x6237;&#x7EBF;&#x7A0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// src/process/mod.rs</span>

<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token punctuation">...</span>
    <span class="token keyword">extern</span> <span class="token string">&quot;C&quot;</span> <span class="token punctuation">{</span>
        <span class="token keyword">fn</span> <span class="token function-definition function">_user_img_start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">fn</span> <span class="token function-definition function">_user_img_end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">let</span> data <span class="token operator">=</span> <span class="token keyword">unsafe</span> <span class="token punctuation">{</span>
        <span class="token namespace">core<span class="token punctuation">::</span>slice<span class="token punctuation">::</span></span><span class="token function">from_raw_parts</span><span class="token punctuation">(</span>
            _user_img_start <span class="token keyword">as</span> <span class="token operator">*</span><span class="token keyword">const</span> <span class="token keyword">u8</span><span class="token punctuation">,</span>
            _user_img_end <span class="token keyword">as</span> <span class="token keyword">usize</span> <span class="token operator">-</span> _user_img_start <span class="token keyword">as</span> <span class="token keyword">usize</span><span class="token punctuation">,</span>
        <span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> user_thread <span class="token operator">=</span> <span class="token keyword">unsafe</span> <span class="token punctuation">{</span> <span class="token class-name">Thread</span><span class="token punctuation">::</span><span class="token function">new_user</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token constant">CPU</span><span class="token punctuation">.</span><span class="token function">add_thread</span><span class="token punctuation">(</span>user_thread<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">...</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x540C;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x8981;&#x4FEE;&#x6539;&#x4E00;&#x4E0B;&#x6784;&#x5EFA;&#x5185;&#x6838;&#x7684; Makefile &#xFF0C;&#x5C06;&#x7528;&#x6237;&#x7A0B;&#x5E8F;&#x94FE;&#x63A5;&#x8FDB;&#x53BB;&#xFF0C;&#x7528;&#x4E4B;&#x524D;&#x63D0;&#x5230;&#x7684;&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre class="language-"><code class="lang-makefile"><span class="token comment"># Makefile</span>
...
<span class="token builtin">.PHONY</span><span class="token punctuation">:</span> kernel build clean qemu run

<span class="token comment"># &#x65B0;&#x589E;</span>
<span class="token keyword">export</span> USER_IMG <span class="token operator">=</span> usr/rust/target/riscv64-rust/debug/hello_world

<span class="token symbol">kernel</span><span class="token punctuation">:</span>
    <span class="token operator">@</span>cargo xbuild --target <span class="token variable">$</span><span class="token punctuation">(</span>target<span class="token punctuation">)</span>.json
...
</code></pre>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC; <code>make run</code> &#x8FD0;&#x884C;&#x4E00;&#x4E0B;&#x8BD5;&#x8BD5;&#x770B;&#xFF0C;&#x53D1;&#x73B0;&#x5185;&#x6838;&#x7EBF;&#x7A0B;&#x4E0E;&#x7528;&#x6237;&#x7EBF;&#x7A0B;&#x80FD;&#x591F;&#x5728;&#x4E00;&#x8D77;&#x5F88;&#x597D;&#x7684;&#x5DE5;&#x4F5C;&#x4E86;&#xFF01;</p>
<pre class="language-"><code class="lang-bash">$ <span class="token function">make</span> run
<span class="token punctuation">..</span><span class="token punctuation">..</span><span class="token punctuation">..</span>
<span class="token operator">&lt;&lt;&lt;</span><span class="token operator">&lt;</span> switch_back to idle <span class="token keyword">in</span> idle_main<span class="token operator">!</span>

<span class="token operator">&gt;&gt;</span><span class="token operator">&gt;&gt;</span> will switch_to thread <span class="token number">5</span> <span class="token keyword">in</span> idle_main<span class="token operator">!</span>
Hello world<span class="token operator">!</span> from user mode program<span class="token operator">!</span>
Hello world<span class="token operator">!</span> from user mode program<span class="token operator">!</span>
Hello world<span class="token operator">!</span> from user mode program<span class="token operator">!</span>
Hello world<span class="token operator">!</span> from user mode program<span class="token operator">!</span>
Hello world<span class="token operator">!</span> from user mode program<span class="token operator">!</span>
Hello world<span class="token operator">!</span> from user mode program<span class="token operator">!</span>
Hello world<span class="token operator">!</span> from user mode program<span class="token operator">!</span>
Hello world<span class="token operator">!</span> from user mode program<span class="token operator">!</span>
Hello world<span class="token operator">!</span> from user mode program<span class="token operator">!</span>
Hello world<span class="token operator">!</span> from user mode program<span class="token operator">!</span>
thread <span class="token number">5</span> exited, <span class="token builtin class-name">exit</span> code <span class="token operator">=</span> <span class="token number">0</span>

<span class="token operator">&lt;&lt;&lt;</span><span class="token operator">&lt;</span> switch_back to idle <span class="token keyword">in</span> idle_main<span class="token operator">!</span>
</code></pre>
<p>&#x81F3;&#x4ECA;&#x4E3A;&#x6B62;&#x7684;&#x6240;&#x6709;&#x4EE3;&#x7801;&#x53EF;&#x4EE5;&#x5728;<a href="https://github.com/rcore-os/rCore_tutorial/tree/ch8-pa4" target="_blank">&#x8FD9;&#x91CC;</a>&#x627E;&#x5230;&#x3002;</p>
<p><div id="page-footer" class="localized-footer"><hr><p><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css"></p>
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<div id="gitalk-container"></div>
<script>
  var gitalk = new Gitalk({
    clientID: "ab871063a9fa7f1da571",
    clientSecret: "4760b384e3e452d2241d859cfa6a7da21a8729ec",
    repo: "rCore_tutorial_doc",
    owner: "rcore-os",
    admin: [
      "chyyuu",
      "xyongcn",
      "wangrunji0408",
      "xy-plus",
      "PanQL",
      "wyfcyx",
      "equation314"
    ],
    id: location.pathname,
    distractionFreeMode: false
  });
  gitalk.render("gitalk-container");
</script>
</div></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="part3.html" class="navigation navigation-prev " aria-label="Previous page: 创建虚拟内存空间">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="part5.html" class="navigation navigation-next " aria-label="Next page: 总结与展望">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"[创建进程]","level":"1.10.5","depth":2,"next":{"title":"总结与展望","level":"1.10.6","depth":2,"path":"chapter8/part5.md","ref":"chapter8/part5.md","articles":[]},"previous":{"title":"创建虚拟内存空间","level":"1.10.4","depth":2,"path":"chapter8/part3.md","ref":"chapter8/part3.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["hide-element","chapter-fold","katex","alerts","emphasize","-highlight","prism","localized-footer","mermaid-gb3"],"pluginsConfig":{"chapter-fold":{},"prism":{"css":["prismjs/themes/prism-tomorrow.css"]},"emphasize":{},"search":{},"localized-footer":{"filename":"extensions/comment/gitalk.html","hline":"true"},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"hide-element":{"elements":[".gitbook-link"]},"katex":{},"fontsettings":{"theme":"white","family":"sans","size":2},"mermaid-gb3":{},"alerts":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"chapter8/part4.md","mtime":"2020-09-09T11:31:56.899Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-09-10T06:57:01.716Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-hide-element/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-mermaid-gb3/book/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    <script src="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.min.js"></script>

    </body>
</html>

